package com.myway.utils;

import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by qianmi-05 on 2018/11/5.
 */
public class RandomUtil {

    /**
     * 根据订单号 0001 递增
     * 生成发票号
     */

     public static String getInvoiceNumber(String flightNo,String invoiceNumber){
         SimpleDateFormat bookingDateSdfTime = new SimpleDateFormat("yyMMdd");
         if(StringUtils.isEmpty(invoiceNumber)||invoiceNumber=="null"){
              return flightNo+"_"+bookingDateSdfTime.format(new Date())+"_"+"0001";
          }
         String[] invoiceNumberArr = invoiceNumber.split("_");
         String today = bookingDateSdfTime.format(new Date());
         if(invoiceNumberArr[0].equals(today)){
             String number = String.valueOf(Integer.parseInt(invoiceNumberArr[1]) + 1);
             if(number.length()==1){
                 number="000"+number;
             }else if(number.length()==2){
                 number="00"+number;
             }else if(number.length()==3){
                 number="0"+number;
             }
             return  flightNo+"_"+invoiceNumberArr[1]+"_"+number;
         };
         return flightNo+"_"+bookingDateSdfTime.format(new Date())+"_"+"0001";
     }

}
